package cn.itcast.n8;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Exchanger;

@Slf4j(topic = "c.TestExchanger")
public class TestExchanger {
    public static void main(String[] args) throws InterruptedException {
        final Exchanger<String> exchanger = new Exchanger<>();

        new Thread(()->{
            try {
                final String t1 = exchanger.exchange("t1...");
                log.debug("t1 = {}",t1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();

        new Thread(() ->{
            try {
                final String t2 = exchanger.exchange("t2...");
                log.debug("t2 = {}",t2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t2").start();

        new Thread(() ->{
            try {
                final String t3 = exchanger.exchange("t3...");
                log.debug("t3 = {}",t3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t3").start();

        new Thread(() ->{
            try {
                final String t4 = exchanger.exchange("t4...");
                log.debug("t4 = {}",t4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t4").start();

    }
}
